﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.Base;

using Menu.Components;

using Xna = Microsoft.Xna.Framework;

#endregion

namespace Menu.Screens
{
    internal enum SplashState
    {
        Start,

        FadeIn,

        Stay,

        FadeOut,
    }

    public class Splash : ScreenBase
    {
        private const float FadeTimeOut = 0.01f;

        private const float FadeValue = 0.015f;

        private const double StartTimeOut = 1;

        private const float StayTimeOut = 1;

        private double elapsedTime;

        private SplashState state;

        private Texture texture;

        public Splash(IScreenChanger changer, object argument)
            : base(changer, argument)
        {
            this.state = SplashState.Start;
        }

        public override ScreenEnum Screen
        {
            get
            {
                return ScreenEnum.Splash;
            }
        }

        public override void LoadContent()
        {
            this.Components = MenuLoader.Get(ScreenEnum.Splash);
            this.texture = (Texture)this.Components["T_Splash"];
        }

        public override void Update(Xna.GameTime gameTime)
        {
            base.Update(gameTime);

            this.elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;

            if (this.StartDelay())
            {
                return;
            }

            if (this.FadeIn())
            {
                return;
            }

            if (this.Stay())
            {
                return;
            }

            if (this.SwitchToFadeOut())
            {
                return;
            }

            this.End();
        }

        private void End()
        {
            if (this.texture.Opacity >= 1 && this.state == SplashState.FadeOut)
            {
                this.RemoveScreen(this.Screen);
                this.AddScreen(ScreenEnum.MainMenu);
            }
        }

        private bool FadeIn()
        {
            if (this.texture.Opacity < 1 && this.elapsedTime > FadeTimeOut && this.state == SplashState.FadeIn)
            {
                this.texture.Opacity += FadeValue;
                this.elapsedTime = 0;
                return true;
            }

            return false;
        }

        private bool StartDelay()
        {
            if (this.elapsedTime > StartTimeOut && this.state == SplashState.Start)
            {
                this.elapsedTime = 0;
                this.state = SplashState.FadeIn;
                return true;
            }

            return false;
        }

        private bool Stay()
        {
            if (this.texture.Opacity >= 1 && this.state == SplashState.FadeIn)
            {
                this.state = SplashState.Stay;
                this.elapsedTime = 0;
                return true;
            }

            return false;
        }

        private bool SwitchToFadeOut()
        {
            if (this.state == SplashState.Stay && this.elapsedTime > StayTimeOut)
            {
                this.state = SplashState.FadeOut;
                this.elapsedTime = 0;
                return true;
            }

            return false;
        }
    }
}